""" A suite of tests to check out a filesystem.  Useful for debugging. """

import os

TREE_0 = [
  'dir_0/',
  'dir_0/subdir_1/',
  'dir_0/subdir_2/',
  'dir_1/',
  'dir_1/subdir_1/',
  'dir_1/subdir_2/',
  'dir_2/',
  'dir_2/dir_0/',
  'file_0',
  'dir_0/file_1',
  'dir_0/subdir_1/file_2',
  'dir_0/subdir_2/file_3',
]

MOVE_1 = [
  'dir_1/',
  'dir_0/subdir_1/',
  'dir_0/subdir_2/',
  'dir_A/',
  'dir_1/subdir_1/',
  'dir_1/subdir_2/',
  'dir_B/',
  'dir_2/dir_1/',
  'dir_1/file_A',
  'file_B',
  'dir_0/subdir_1/file_C',
  'dir_0/subdir_1/file_D',
]

TREE_1 = [
  'dir_1/',
  'dir_1/subdir_1/',
  'dir_1/subdir_2/',
  'dir_A/',
  'dir_A/subdir_1/',
  'dir_A/subdir_2/',
  'dir_B/',
  'dir_B/dir_1/',
  'dir_A/file_A',
  'file_B',
  'dir_1/subdir_1/file_C',
  'dir_1/subdir_1/file_D',
]

print 'TEST create'

# Create the tree
for item in TREE_0:
  print '   c:', item
  if item.endswith('/'):
    os.mkdir(item) 
  else:
    open(item, 'w').write(item)

# Verify the tree
for item in TREE_0:
  print '   v:', item
  if item.endswith('/'):
    os.listdir(item)
  else:
    content = open(item).read()
    if content != item:
      print 'read', content
      print 'should be', item
      raise StringException('Verification')

print '  PASSED'

print 'TEST move'

# Move the tree around, NOTE reverse order
assert len(TREE_0) == len(TREE_1)
for i in range(len(MOVE_1) - 1, -1, -1):
  print '   m:', TREE_0[i], MOVE_1[i]
  os.rename(TREE_0[i], MOVE_1[i])

# Verify the moved tree
for i in range(len(TREE_0)):
  print '   v:', item
  if TREE_1[i].endswith('/'):
    os.listdir(TREE_1[i])
  else:
    content = open(TREE_1[i]).read()
    if content != TREE_0[i]:
      print 'read', content
      print 'should be', item
      raise StringException('Verification')

print '  PASSED'

print 'TEST remove'

# Delete the tree
for item in reversed(TREE_1):
  print '   rm:', item
  if item.endswith('/'):
    os.rmdir(item)
  else:
    os.remove(item) 

# Verify everything is gone
if os.listdir('.'):
  raise StringException('FAILED: not empty after cleanup')

print '  PASSED'

print 'TEST copy'

os.mkdir('dir_1')
os.mkdir('dir_2')
open('dir_1/file_1', 'w').write('dir_1/file_1')
os.link('dir_1/file_1', 'file_A')
os.link('dir_1/file_1', 'dir_1/file_B')
os.link('dir_1/file_1', 'dir_2/file_C')
assert open('file_A').read() == 'dir_1/file_1'
assert open('dir_1/file_B').read() == 'dir_1/file_1'
assert open('dir_2/file_C').read() == 'dir_1/file_1'
os.remove('file_A')
os.remove('dir_1/file_B')
os.remove('dir_2/file_C')
os.remove('dir_1/file_1')
os.rmdir('dir_1')
os.rmdir('dir_2')

# Verify everything is gone
if os.listdir('.'):
  raise StringException('FAILED: not empty after cleanup')

print '  PASSED'
